<template>
  <el-container>
    <el-header>
      <div>
        <img class="logo" src="../assets/logo.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="signout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu
          @open="handleOpen"
          @close="handleClose"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
        >
          <!--使用双层for循环来渲染菜单 -->
          <el-submenu :index="item.id+'' " v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="itemson.id + ''"
              v-for="itemson in item.children"
              :key="itemson.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{itemson.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>Main</el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    //页面私有数据区，定义一个menuList，用来装请求到的菜单数据
    return {
      menuList: [],
      //定义一个图标对象(key,value类型)  对象{type:"porsche", model:"911", color:"white"}，数组[1,2,3] (数组中间是:不是=)
      iconObj: {
        '101': '',
        '102': '',
        '103': '',
        '125': '',
        '145': ''  
      }
    }
  },

  //这个主页一打开就去请求菜单数据，所以使用生命周期 created(实例创建完成 )
  created() {
    this.getMenuList() //调用getMenuList()
  },
  methods: {
    signout() {
      window.sessionStorage.clear()
      this.$router.push('/Login') //编程式导航
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath)
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus') //.get()返回的是一个promise. 解构赋值出一个data值
      console.log('home页面请求的菜单数据', res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.status)
      //成功就给该也的私有数据赋值
      this.menuList = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center; //居中对齐弹性盒的各项 <div> 元素：<-----******
  color: #fff; // 字体颜色 灰色
  font-size: 20px;
  > div {
    // >这是css的子元素选择器
    //这是里面的嵌套
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-container {
  background-color: #eaedf1;
  height: 100%;
}
.logo {
  height: 60px;
  width: 60px;
}
</style>
